/*************************************************
 * File Name: Scenario.java
 * Package Name: com.sensorfriendly.model
 * 
 * Purpose:
 * This file contains the structure of the Scenarios
 * which includes the:
 *  - Scenario Name
 *  - List of Messages associated with it
 *  - The Icon associated with it
 *  
 * And all the setter and getter functions along
 * with the editMessage function
 *  
 *************************************************/

package com.sensorfriendly.model;

import java.util.ArrayList;
import java.util.List;

public class Scenario {
	private List<Message> messages;
	private String name;
	private String iconPath; 
	private Long timeCreated;
	
	
	
	public Scenario(){
	}
	
	
	public Scenario(String sName){
		this.name = sName;
	}
	
	public Scenario(String sName, List<Message> msgs){
		this.messages = msgs;
		this.name = sName;
	}
		
	public void addMessage(Message msg){
		if(messages == null){
			messages = new ArrayList<Message>();
		}
		messages.add(msg);
	}

	public List<Message> getMessages() {
		return messages;
	}

	public void setMessages(List<Message> messages) {
		this.messages = messages;
	}
	
	public void deleteMessage(String msg){
		for(Message m : messages){
			if (m.getMessageContent().equalsIgnoreCase(msg)) {
				messages.remove(m);
				break;
			}
		}
	}

	public String getName() {
		return name;
	}
	public void setName(String scenarioName) {
		this.name = scenarioName;
	}
	public String getIconPath() {
		return iconPath;
	}
	public void setIconPath(String iconPath) {
		this.iconPath = iconPath;
	}

	public Long getTimeCreated() {
		return timeCreated;
	}

	public void setTimeCreated(Long timeCreated) {
		this.timeCreated = timeCreated;
	}

	@Override
	public String toString(){
		return name+"";
	}
	
	
	public void editMessage(String currentMsg, String newMsg){
		for(Message m : messages){
			if (m.getMessageContent().equalsIgnoreCase(currentMsg)) {
				m.setMessageContent(newMsg);
				break;
			}
		}
	}
	
	
	public boolean editMessage(String currentMsg, String newMsg, String audioPath){
		Message toEdit = null;
		boolean duplicate = true;
		for(Message m : messages){
			if(m.getMessageContent().equalsIgnoreCase(newMsg) && !currentMsg.equalsIgnoreCase(newMsg)){
				duplicate = true;
				break;
			}
			if (m.getMessageContent().equalsIgnoreCase(currentMsg)) {
				toEdit = m;
				duplicate = false;
			}
		}
		if(!duplicate && toEdit != null){
			toEdit.setMessageContent(newMsg);
			if(audioPath != null){
				toEdit.setAudioFile(audioPath);
			}
		}
		return !duplicate;
	}
	
}
